<?php

// Time Zone - sets time to Central Time
date_default_timezone_set("America/Los_Angeles");
error_reporting (E_ALL ^ E_NOTICE);
//mysqli_report(MYSQLI_REPORT_OFF); 
//////////////////////////////////////////


/* As of  php 5.5.0 mysql connect became deprecated. This will be the replacement 
 * for the db_connect using the mysqli intereface
 * to interact with the mysql
 */
class dbConnection
{
    /**************************************************************************
     * Memebers
     *************************************************************************/
    /**
     * database host/ip
     * @var string*/
    private $host ="localhost";
    /**
     * database user name
     * @var string*/
    private $user = "db username"; 
     /**
     * database password
     * @var string*/
    private $password = "password";
    /**
     * which db we are accessign on server
     * @var string*/
    private $mydb="db name";
    /**
     * mysqli instance
     * @var object*/
    public  $con;
    
    /***
     * Create a connection in constructor to db
     */
    function __construct() {
        $aCon = mysqli_connect($this->host, $this->user, $this->password, $this->mydb);
        if(mysqli_errno($aCon)){
            echo "Failed to connect to MySQL using mysqli: " . $aCon->connect_error;  
        }
        else{
           $this->con = $aCon; // assign $con 
        } 
    }
    
    /***
     * Close a connection in destructor
     */
     function __destruct() {
        $this->con->close();
     }
    /******************************************************************************
     * I added the following function to have error handling simplified and to 
     * reduce  repeated code
     * 
     * Dont forget to check all user inputted data with:
     *    $this->con->real_escape_string($var);
     *****************************************************************************/
    public function insert($sql){
        $this->con->query($sql);
        $this->echoError($sql);
    }
    
    public function update($sql){
        if (! $this->con->query($sql)) {
            $this->echoError($sql);
        }
    }
    
    public function select($sql){
       $res = $this->con->query($sql);
       $this->echoError($sql);
       return $res;
    }
    
    /**
     * This functions kills a page and echos an error during a query
     */
    private function echoError($sql){
        if ($this->con->error) {
            //i have to wait till update to the new version of php for exception 
            //error class... for the future
            try {    
                throw new Exception("MySQL error: <br> "
                    . "Query:<br> $sql<br>Please send this info to the "
                    . "<a href='mailto:robert.w.preston@navy.mil'>Webmaster</a>"
                    . "<br><h1>Sorry  <a href='http://".$_SERVER['SERVER_NAME']."/index.php"."'>Click Here </a>"
                    . "To return to Home<br><br>", $this->con->errorno);    
            }   
            catch(Exception $e ) {
                echo "Error No: ".$e->getCode(). " - ". $e->getMessage() . "<br >";
                echo nl2br($e->getTraceAsString());
                return;
            }
        }
    }
    /******************************************************************************/
}


